<template>
	<div class="home">
	
		<!-- charts -->
		<el-row :gutter="15" class="mt15">
			<el-col :md="24" :lg="8" :xl="8" class="mb15">
				<el-card shadow="hover">
					<div slot="header">
						<span>今日订单情况</span>
						<!-- <span>{{ $t('message.card.title6') }}</span> -->
					</div>
					<div class="charts">
						<div class="charts-right">
							<div ref="homeStockRef" class="h100"></div>
						</div>
					</div>
				</el-card>
			</el-col>
			
			<el-col :md="24" :lg="16" :xl="16" class="mb15">
				<el-card shadow="hover">
					<div slot="header">
						<span>热销菜品</span>
						<!-- 热销菜品 -->
						<!-- <span>{{ $t('message.card.title7') }}</span> -->
					</div>
					<div class="charts">
						<div class="charts-left mr7">
							<div ref="homeLaboratoryRef" class="h100"></div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>

		<!-- v-charts -->
		<el-row :gutter="15">
			<el-col :md="24" :lg="16" :xl="16" class="home-lg">
				<el-card shadow="hover">
					<div slot="header">
						<span>每月利润</span>
						<!-- <span>{{ $t('message.card.title8') }}</span> -->
					</div>
					<div class="charts">
						<div class="charts-left mr7">
							<div ref="homeOvertimeRef" class="h100"></div>
						</div>
					</div>
				</el-card>
			</el-col>
			<el-col :md="24" :lg="8" :xl="8">
				<el-card shadow="hover">
					<div slot="header">
						<!-- <span>{{ $t('message.card.title9') }}</span>
						 -->
						 <p>订单数</p>
					</div>
					<!-- 已完成的订单，按照年月日 -->
					<div class="home-charts">
						<div class="home-charts-item" v-for="(v, k) in finshedOrder" :key="k">
							<div class="home-charts-item-left">
								<div class="home-charts-item-title">{{ v.title }}</div>
								<div class="home-charts-item-num" :style="{ color: v.color }" >{{ v.num }}</div>
								<!-- <div class="home-charts-item-title">{{ v.title }}</div> -->
								<!-- <div class="home-charts-item-num" :style="{ color: v.color }" :id="`titleNum${k + 1}`"></div> -->
							</div>
				
							<div class="home-charts-item-right">
								<i :class="v.icon" :style="{ 'background-color': v.iconBg, color: v.color }"></i>
							</div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script>
import * as echarts from 'echarts';
import Scroll from 'vue-seamless-scroll';
import { CountUp } from 'countup.js';
import { Session } from '@/utils/storage';
import { formatAxis, formatDate } from '@/utils/formatTime';
import { recommendList, chartsRightList, newsInfoList, dailyMessage } from './mock';
import axios  from 'axios';
export default {
	name: 'home',
	components: { Scroll },
	data() {
		return {
			recommendList,
			chartsRightList,
			newsInfoList,
			userInfo: {},
			dailyMessage: {},
			finshedOrder:[{
					title:"今年已完成订单",
					num:0
				},{
					title:"本月已完成订单",
					num:0
				},{
					title:"今天已完成订单",
					num:0
				}
			],
			requesturl:"http://localhost:8086/",//axios请求地址
			nowdaynum:[{}],
			orderState:[0,0,0],//获取
		};
	},
	created() {
		this.initUserInfo();
		this.initDailyMessage();
	},
	computed: {
		currentTime() {
			return formatAxis(new Date());
		},
		optionSingleHeight() {
			return {
				singleHeight: 28,
				limitMoveNum: 8,
				waitTime: 2000,
			};
		},
		getUserInfos() {
			return this.$store.state.userInfos.userInfos;
		},
	},
	mounted() {
		//PC得到完成的订单数
		this.getFinshOrder();

		//PC得到今天的订单情况
		this.getNowDayOrderState();

		//PC得到卖的最多的六个产品
		this.getTopFoodName();

		//PC得到月份营业额 月份订单数
		this.getMonthMoney();

		// this.initHomeStock();//当天订单状态图表
		// this.initHomeLaboratory();//echar食品排行
		// this.initHomeOvertime();//第三个echar月份的主食
		this.initNumCountUp();
		
		
	},
	methods: {
		//PC获得当前完成订单
		getFinshOrder(){
			axios.get(this.requesturl+"PC/getOrderFinshed").then(res=>{

				this.finshedOrder[0].num=res.data.data[0]
				this.finshedOrder[1].num=res.data.data[1]
				this.finshedOrder[2].num=res.data.data[2]
			})

		},
		//PC获得今天的订单情况
		getNowDayOrderState(){
			axios.get(this.requesturl+"PC/CountOrderState").then(res=>{
				
				this.orderState=res.data.data
				
				const myChart = echarts.init(this.$refs.homeStockRef);
				const option = {
					grid: {
						top: 50,
						right: 20,
						bottom: 30,
						left: 30,
					},
					tooltip: {
						trigger: 'item',
					},
					legend: {
						left: 'center',
					},
					series: [
						{
							name: '订单详情',
							type: 'pie',
							radius: ['40%', '70%'],
							avoidLabelOverlap: false,
							itemStyle: {
								borderRadius: 10,
								borderColor: '#fff',
								borderWidth: 2,
							},
							data: [
								{ value: this.orderState[0], name: '顾客下单' },
								{ value: this.orderState[1], name: '商家接单' },
								{ value: this.orderState[2], name: '顾客付款' },
							],
								top: 30,
							},
						],
					};
					myChart.setOption(option);
					window.addEventListener('resize', () => {
						myChart.resize();
					});

			})
		},
		//PC获得热销菜品
		getTopFoodName(){
			axios.get(this.requesturl+"PC/getTopFood").then(res=>{
				let foodnameArr=[]
				let foodcountArr=[]
				console.log(res.data.data);
				res.data.data.forEach(item => {
					foodnameArr.push(item.foodname)
					foodcountArr.push(item.countfood)
				});
				console.log("打印食品数组",foodnameArr);
				const myChart = echarts.init(this.$refs.homeLaboratoryRef);
				const option = {
					grid: {
						top: 50,
						right: 20,
						bottom: 30,
						left: 30,
					},
					tooltip: {
						trigger: 'axis',
					},
					legend: {
						data: ['最新成交量'],
						right: 13,
					},
					xAxis: {
						data: foodnameArr,
					},
					yAxis: [
						{
							type: 'value',
							name: '数量',
						},
					],
					series: [
						// {
						// 	name: '预购队列',
						// 	type: 'bar',
						// 	data: [5, 20, 36, 10, 10, 20],
						// },
						{
							name: '最新成交量',
							type: 'line',
							data: foodcountArr,
						},
					],
				}
				myChart.setOption(option);
				window.addEventListener('resize', () => {
					myChart.resize();
				});
			})
		},
		//获得每个月的订单和营业额
		getMonthMoney(){
			axios.get(this.requesturl+"PC/getMonthMoney").then(res=>{
				let mo=res.data.data 
				let monthMoney=[]
				let monthOrder=[]
				for (let index = 0; index < mo.length; index++) {
					monthMoney[index] = mo[index].monthMoney
					monthOrder[index] = mo[index].monthOrder
				}
				const myChart = echarts.init(this.$refs.homeOvertimeRef);
			const option = {
				grid: {
					top: 50,
					right: 20,
					bottom: 30,
					left: 30,
				},
				tooltip: {
					trigger: 'axis',
				},
				legend: {
					// data: ['订单数量', '销售额'],
					data: ['销售额'],
					right: 13,
				},
				xAxis: {
					data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
				},
				yAxis: [
					{
						type: 'value',
						name: '销售额',
						// interval:2000
					},
				],
				series: [
					// {
					// 	name: '订单数量',
					// 	type: 'line',
					// 	data: monthOrder,
					// },
					{
						name: '销售额',
						type: 'bar',
						data: monthMoney,
					},
					
					
				],
			};
				myChart.setOption(option);
				window.addEventListener('resize', () => {
					myChart.resize();
				});
			})
		},
		// 初始化数字滚动
		initNumCountUp() {
			this.$nextTick(() => {
				new CountUp('titleNum1', Math.random() * 100000).start();
				new CountUp('titleNum2', Math.random() * 100000).start();
				new CountUp('titleNum3', Math.random() * 100000).start();
			});
		},

		// 随机语录
		initDailyMessage() {
			this.dailyMessage = dailyMessage[Math.floor(Math.random() * dailyMessage.length)];
		},
		// 初始化登录信息
		initUserInfo() {
			if (!Session.get('userInfo')) return false;
			this.userInfo = Session.get('userInfo');
			this.userInfo.time = formatDate(new Date(this.userInfo.time), 'YYYY-mm-dd HH:MM:SS');
		},
		// 消息通知当前项点击
		onNewsInfoListClick(v) {
			window.open(v.link);
		},
		// 跳转到 gitee
		// onOpenGitee() {
		// 	window.open('https://gitee.com/lyt-top/vue-next-admin');
		// },
		
	},
};
</script>

<style scoped lang="scss">
@import './index.scss';
</style>
